<?php
require_once dirname(__FILE__) . '/common.inc.php';
require_once dirname(__FILE__) . "/DbUtils.class.php";

header('Content-Type: text/plain');

$track_id = MiscUtils::getParam('i', NULL);
$tablename = MiscUtils::getParam('t', NULL);
$attrname = MiscUtils::getParam('n', NULL);
$attrpath = MiscUtils::getParam('p', NULL);
$folder = MiscUtils::getParam('f', NULL);


$myPdo = DbUtils::createPdoInst();
$cond_vals = new stdClass();
$cond_vals->c = 't.track_id = :v1';
$cond_vals->v = array(':v1' => $track_id);
$result = DbUtils::get($myPdo, $tablename, $cond_vals, NULL, NULL, NULL, NULL, NULL);
if (count($result->d) != 1) {
  die('File not found in database');
}
$obj = $result->d[0];
$path = $folder . (($attrpath) ? $obj->$attrpath : '');
if (!file_exists($path)) {
  echo 'File not found in hard drive';
  exit;
} else {
  $file = fopen($path, "r");
  Header("Content-type: application/octet-stream");
  Header("Accept-Ranges: bytes");
  Header("Accept-Length:" . filesize($path));
  Header("Content-Disposition: attachment; filename=\"" . urlencode($obj->$attrname) . "\"");
  echo fread($file, filesize($path));
  fclose($file);
  exit;
}
?>